<?php

namespace App\Http\Controllers\Admin\Finance;

use App\Http\Controllers\Controller;
use App\Http\Requests\finance\AgentInvoiceRequest;
use App\Models\finance\AgentInvoiceModel;
use Illuminate\Http\Request;

class AgentInvoice extends Controller
{
    use AgentInvoiceRequest;

    /**
     * Notes:列表
     * User: xiangzi
     * Date: 2024/9/5
     * Time: 14:35
     * @return \Illuminate\Http\JsonResponse
     */
    public function list(Request $request) {
        $page = $request->input('page', 1);
        $limit = $request->input('limit', 20);
        $company_name = $request->input('company_name', '');
        $type = $request->input('type', '');
        $cycle_start = $request->input('cycle_start', '');
        $cycle_end = $request->input('cycle_end', '');

        $query = AgentInvoiceModel::query()->with(['create_admin', 'agent']);

        if($company_name){
            $query->where('company_name', 'like', '%'.$company_name.'%');
        }

        if($type){
            $query->where('type', $type);
        }

        if($cycle_start && $cycle_end){
            $query = $query->where('cycle_start', '>=', $cycle_start)->where('cycle_end', '<=', $cycle_end);
        }

        $total = $query->count('id');
        $items = $query
            ->offset(($page-1) * $limit)
            ->limit($limit)
            ->orderBy('id', 'desc')
            ->get();

        $list = $items->map(function($item) {
            $res = [
                'id' => $item->id,
                'agent_name' => $item->agent ? $item->agent->name : '',
                'company_name' => $item->company_name,
                'type' => $item->type,
                'type_name' => AgentInvoiceModel::TYPE[$item->type],
                'amount' => $item->amount,
                'tax_number' => $item->tax_number,
                'open_bank' => $item->open_bank,
                'bank_account' => $item->bank_account,
                'company_address' => $item->company_address,
                'company_mobile' => $item->company_mobile,
                'remarks' => $item->remarks,
                'created_at' => $item->created_at->toDateTimeString(),
                'create_user' => $item->create_admin ? $item->create_admin->staff->name: '',
            ];
            return $res;
        });

        return $this->success(['list' => $list, 'total' => $total]);
    }

    /**
     * Notes:添加
     * User: xiangzi
     * Date: 2024/9/5
     * Time: 14:35
     * @return \Illuminate\Http\JsonResponse
     */
    public function add(Request $request){
        $auth = $request->get('auth');
        $params = $request->all();

        $check_res = self::add_check($params);
        if($check_res->faild){
            return $this->error($check_res->err);
        }

        $model = new AgentInvoiceModel();
        $model->agent_id = $params['agent_id'];
        $model->company_name = $params['company_name'];
        $model->type = $params['type'];
        $model->tax_number = $params['tax_number'];
        $model->open_bank = $params['open_bank'];
        $model->bank_account = $params['bank_account'];
        $model->company_address = $params['company_address'];
        $model->company_mobile = $params['company_mobile'];
        $model->amount = $params['amount'];
        $model->cycle_start = $params['cycle_start'];
        $model->cycle_end = $params['cycle_end'];
        $model->remarks = isset($params['remarks']) ? $params['remarks'] : '';
        $model->created_admin_id = $auth->id;
        $model->save();

        return $this->success();
    }

}
